package Lab12;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URLConnection;

public class Ex12_4 {
    public static void main(String[] args) {
        String URLString = "https://www.douban.com/robots.txt";
        try {
            java.net.URL url = new java.net.URL(URLString);
            int count=0;
            URLConnection connection = url.openConnection();

            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                count += line.split(" ").length;
            }
//            The number of words is 68

            System.out.println("The number of words is " + count);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
